<template>
  <qx-table :dbRowClick="rowDblClick" :option="tableOption" :remoteData="remoteData">
    <template v-slot:top>
      <qx-form v-model="searchForm" :option="tableOption" isSearch @submit="startSearch"></qx-form>
    </template>
    <template v-slot:menuLeft>
      <q-btn color="primary" icon="add" unelevated @click="handleCreate">
        {{ $t('Add') }}
      </q-btn>
      <q-import :errorParam="['YFAGV', 'importAgvCfgRcs']" downUrl="/yfagv/api/agvCfgRcs/importInfoTemplate"
                importUrl="/yfagv/api/agvCfgRcs/importInfo" @import-after="startSearch"></q-import>
    </template>
    <template v-slot:menuLeftR>
      <q-icon color="warning" name="mdi-map-marker-question cursor-pointer cursor-hover" size="1.5rem">
        <q-tooltip class="q-pa-md" v-bind="$tooltipAttr">{{ $t('Misc.DblClickCardTips') }}</q-tooltip>
      </q-icon>
    </template>
    <template v-slot:menu="{ row }">
      <q-btn class="col" color="primary" icon="mdi-square-edit-outline" outline unelevated @click="handleUpdate(row)">
        {{ $t('Edit') }}
      </q-btn>
      <q-btn class="col" color="primary" icon="delete" outline unelevated @click="handleDelete(row)">
        {{ $t('Del') }}
      </q-btn>
    </template>
  </qx-table>
  <q-modal v-model="modalVisible" :header="formInfo.id ? $t('Edit') : $t('Add')" :width="500" btnAlign="center"
           position="right" @beforeClose="beforeClose" @confirm="submitForm">
    <qx-form ref="FormRef" v-model="formInfo" :option="tableOption"></qx-form>
  </q-modal>
  <q-modal v-model="infoVisible" :header="`${$t('Misc.SpecifyDetails')}`" :menuBtn="false" :persistent="false"
           width="620">
    <QInfo :columns="tableOption.column || []" :rowInfo="rowInfo" cols="2" headerWidth="100"></QInfo>
  </q-modal>
</template>
<script setup>
import { i18n, toI18nName } from 'boot/i18n.js'
import { deleteAPI, postAPI, putAPI } from 'src/api/index.js'
import { getRemoteData, setRemoteData } from 'src/enum/ajaxRemoteData.js'
import { enableOrDisable, yesOrNo } from 'src/enum/index.js'
import { computed, onMounted, ref } from 'vue'
import { createDialog, deepClone } from 'src/utils/utils.js'

const rowInfo = ref({})
const formInfo = ref({})
const FormRef = ref(null)
const AGVBrands = ref([])
const searchForm = ref({})
const infoVisible = ref(false)
const modalVisible = ref(false)
const remoteData = ref({ dicUrl: '/yfagv/api/agvCfgRcs/list' })
getRemoteData('agv_brand', '/yfagv/api/agvCfgBrand/list').then(data => (AGVBrands.value = data))
const tableOption = computed(() => {
  return {
    grid: true, span: 22, export: true, labelWidth: 100, searchSpan: 4, headerWidth: 60,
    searchLabelWidth: 60,
    column: [{
      label: `${i18n.t('AGVCore.AGVBrand')}`, field: 'agvBrandCode', type: 'select', options: AGVBrands.value,
      minWidth: 100, props: { label: `${toI18nName('agvBrandNameZhCn')}`, value: 'agvBrandCode' },
      required: true
    }, {
      label: `${i18n.t('RCSCore.RCSProtocol')}`, field: 'protocol', type: 'select', required: true,
      options: ['http', 'https'], minWidth: 100, hide: true, export: false, showInfo: false
    }, {
      label: `${i18n.t('RCSCore.RCSAddress')}`, field: 'host', required: true, minWidth: 70, search: false,
      format: (_, row) => `${row.protocol || ''}://${row.host || ''}:${row.port || ''}`
    }, {
      label: `${i18n.t('RCSCore.RCSPort')}`, field: 'port', required: true, minWidth: 70, hide: true,
      search: false, export: false, showInfo: false
    }, {
      label: `${i18n.t('RCSCore.RCSVersion')}`, field: 'agvRcsVersion', minWidth: 80, hide: true, topLeft: true
    }, {
      label: 'Token', field: 'authToken', minWidth: 80, search: false
    }, {
      label: `${i18n.t('BuiltIn')}`, field: 'sysInner', search: false, display: false, options: yesOrNo.value,
      useTag: true, minWidth: 80
    }, {
      label: `${i18n.t('IsEnable')}`, field: 'isEnable', type: 'radio', searchType: 'select', value: '1',
      options: enableOrDisable.value, useTag: true, topRight: true
    }, {
      label: `${i18n.t('CreationTime')}`, field: 'createdTime', display: false, minWidth: 80, search: false
    }, {
      label: `${i18n.t('Remarks')}`, field: 'remark', type: 'textarea', minWidth: 100, search: false, hide: true
    }]
  }
})

const handleCreate = () => {
  modalVisible.value = true
}
const rowDblClick = row => {
  rowInfo.value = row
  infoVisible.value = true
}
const handleUpdate = row => {
  formInfo.value = deepClone(row)
  modalVisible.value = true
}
const handleDelete = row => {
  createDialog({ msg: `${i18n.t('DelTips')}` }, () => {
    deleteAPI(`/yfagv/api/agvCfgRcs/${row.id}`, {}, () => startSearch('clear'))
  }, () => {})
}
const formatSearchForm = () => ({ ...searchForm.value })
const startSearch = (action, done) => {
  remoteData.value.params = formatSearchForm()
  remoteData.value.clearSelected = true
  if (done) done()
  action === 'clear' && setRemoteData('rcs_address')
}
const beforeClose = () => {
  if (FormRef.value) FormRef.value.onReset()
  modalVisible.value = false
}
const submitForm = mDone => {
  FormRef.value.validate((form, done) => {
    const API = form.id ? putAPI : postAPI
    API('/yfagv/api/agvCfgRcs', form, () => {
      startSearch('clear')
      done()
      mDone && mDone()
    }, () => {
      done()
      mDone && mDone(false)
    })
  }, () => mDone && mDone(false))
}
onMounted(startSearch)
</script>